//
//  XNotification.swift
//  ZGKB
//
//  Created by apple on 2019/12/4.
//  Copyright © 2019 easyshare. All rights reserved.
//

import UIKit

//通知名字扩展
public extension Notification.Name {
    ///跳转登录
    static let XNotificationNeedLogin = Notification.Name(rawValue:"XNotificationNeedLogin")
    ///登录成功
    static let XNotificationLoginSuccessed = Notification.Name(rawValue:"XNotificationLoginSuccessed")
    ///退出登录成功
    static let XNotificationLogoutSuccessed = Notification.Name(rawValue:"XNotificationLogOutSuccessed")
    /// 用户信息更新
    static let XNotificationUserInfoChanged = Notification.Name(rawValue:"XNotificationUserInfoChanged")
    

}

public class LXNotification: NSObject {
    public let notificationCenter: NotificationCenter
    public let token: Any
    public let observerName :String?
    public init(notificationCenter: NotificationCenter = .default, token: Any, name:String?) {
        self.notificationCenter = notificationCenter
        self.token = token
        self.observerName = name
    }

    deinit {
        notificationCenter.removeObserver(token)
        LXLog("removeObserver: \(observerName ?? "none")")
    }

    public static func add(name: NSNotification.Name?, object obj: Any?, using block: @escaping (Notification) -> ()) -> LXNotification{
        return NotificationCenter.default.x_add(name: name, object: obj, queue: .main, using: block)
//        NotificationCenter.default.addObserver(forName: name, object: obj, queue: .main, using: block)
    }
    public static func remove(observer: Any) {
        NotificationCenter.default.removeObserver(observer)
    }
    public static func remove(observer: Any, name:NSNotification.Name?) {
        NotificationCenter.default.removeObserver(observer, name: name, object: nil)
    }
    public static func post(name: NSNotification.Name, object: Any?) {
        NotificationCenter.default.post(name: name, object: object)
    }
    public static func post(name: NSNotification.Name) {
        NotificationCenter.default.post(name: name, object: nil)
    }
    public static func post(name: NSNotification.Name, object: Any?, userInfo:[AnyHashable : Any]?) {
        NotificationCenter.default.post(name: name, object: object, userInfo: userInfo)
    }
    
    public static func post(name: NSNotification.Name, userInfo:[AnyHashable : Any]?) {
        NotificationCenter.default.post(name: name, object: nil, userInfo: userInfo)
    }

}

public final class LXNotificationToken: NSObject {
    public let notificationCenter: NotificationCenter
    public let token: Any
    public let observerName :String?
    public init(notificationCenter: NotificationCenter = .default, token: Any, name:String?) {
        self.notificationCenter = notificationCenter
        self.token = token
        self.observerName = name
    }

    deinit {
        notificationCenter.removeObserver(token)
        LXLog("removeObserver: \(observerName ?? "none")")
    }
}

public extension NotificationCenter{
    
    /// 自动销毁的Observer
    func x_add(name: NSNotification.Name?, object obj: Any?,
    queue: OperationQueue?, using block: @escaping (Notification) -> ())
    -> LXNotification
    {
        let token = addObserver(forName: name, object: obj, queue: queue, using: block)
        return LXNotification(notificationCenter: self, token: token,name: name?.rawValue)
    }

}
